package com.zcx.service;

import com.zcx.mapper.FileMapper;
import com.zcx.pojo.vo.FileVo;
import com.zcx.pojo.vo.JSONResult;
import com.zcx.utils.SimHash;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileService {

    @Autowired
    FileMapper fileMapper;

    @Autowired
    JSONResult jsonResult;

    private List<FileVo> duplicateList = new ArrayList<>();

    public JSONResult readFile(MultipartFile uploadfile,String completePath) throws IOException {

        InputStream is = uploadfile.getInputStream();
        String filename = uploadfile.getOriginalFilename();

        String resullt = null;

        try {
            if (filename.endsWith(".doc")) {
                WordExtractor re = new WordExtractor(is);
                resullt = re.getText().replaceAll("\r\n", "").replaceAll("\\p{Punct}|\\p{Space}","");
                re.close();
                System.out.println(resullt);
            } else if (filename.endsWith(".docx")) {
                OPCPackage opcPackage = POIXMLDocument.openPackage(completePath);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                resullt = extractor.getText().replaceAll("\r\n", "").replaceAll("\\p{Punct}|\\p{Space}","");
                extractor.close();
                System.out.println(resullt);
            } else {
                System.out.println("此文件不是word文件");
                return jsonResult.error("必须上传world文档");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        SimHash simHash = new SimHash();
        String simhash = simHash.calSimhash(resullt);
        List<FileVo> files = fileMapper.find();

        for (FileVo file : files) {
            System.out.println("数据库中的文本:"+file.getId()+"-----当前文本:"+simhash);
            boolean duplicate = simHash.isDuplicate(file.getId(), simhash);
            if(duplicate){
                duplicateList.add(file);
            }
        }

        if(duplicateList.isEmpty()){
            FileVo fileVo = new FileVo();
            fileVo.setId(simhash);
            fileVo.setFileName(filename);
            fileVo.setAuth(filename);
            fileMapper.save(fileVo);
        }else {
            FileUtils.deleteQuietly(new File(completePath));
            this.duplicateList.clear();
            return jsonResult.error("该文档与其它文档相似度过高");
        }

//        FileUtils.copyToFile(uploadfile.getInputStream(),new File(completePath));

        return jsonResult.ok("审核通过")
                .put("duplicateFiles",duplicateList);
    }

}
